package com.hl.ruoyicsmshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hl.ruoyicsmshop.domain.User;
import com.hl.ruoyicsmshop.service.LoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private TokenService tokenService;

//    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @RequestMapping("/loginForId")
    public R loginForId(@RequestBody User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nickname", user.getUserNickname());
        queryWrapper.eq("password", user.getUserPassword());
        User user1 = loginService.loginForId(user.getUserNickname(), user.getUserPassword());
        if (user1 == null){
            return R.fail("登录失败!");
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserId(user1.getUserId());
        sysUser.setUserName(user1.getUserNickname());
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(user1.getUserNickname());
        loginUser.setBusinessId(user1.getUserId());
        loginUser.setSysUser(sysUser);

        return R.ok(tokenService.createToken(loginUser));
    }
}
